package com.tech.utils;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.util.Calendar;
import java.util.Date;

/**
 * Created by admin on 2017/9/14.
 */
public class DateUtil {
    private static final DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
    private static final DateTimeFormatter formatDay = DateTimeFormat.forPattern("yyyy-MM-dd");
    private static final DateTimeFormatter DATE_FORMAT_DATEONLY = DateTimeFormat.forPattern("MM-dd");
    private static final DateTimeFormatter DATE_FORMAT_TIMEONLY = DateTimeFormat.forPattern("HH:mm");
    private static final DateTimeFormatter DATE_FORMAT_HMS = DateTimeFormat.forPattern("HH:mm:ss");
    private static final DateTimeFormatter DATE_FORMAT_Y = DateTimeFormat.forPattern("yyyy");
    private static final DateTimeFormatter DATE_FORMAT_YMDHMS = DateTimeFormat.forPattern("yyyyMMddHHmmss");
    private static final DateTimeFormatter DATE_FORMAT_YMD = DateTimeFormat.forPattern("yyyyMMdd");


    public static Date parseYMDHMS(String arg) {
        DateTime dateTime = DateTime.parse(arg, format);
        return dateTime.toDate();
    }

    public static String formatYMDHMS(Date date) {
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(format);
    }

    public static Date parseYMD(String arg) {
        DateTime dateTime = DateTime.parse(arg, formatDay);
        return dateTime.toDate();
    }

    public static String formatYMD(Date date) {
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(formatDay);
    }

    public static Date parseMD(String arg) {
        DateTime dateTime = DateTime.parse(arg, DATE_FORMAT_DATEONLY);
        return dateTime.toDate();
    }

    public static String formatMD(Date date) {
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(DATE_FORMAT_DATEONLY);
    }

    public static Date parseHM(String arg) {
        DateTime dateTime = DateTime.parse(arg, DATE_FORMAT_TIMEONLY);
        return dateTime.toDate();
    }

    public static String formatHM(Date date) {
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(DATE_FORMAT_TIMEONLY);
    }

    public static Date parseHMS(String arg) {
        DateTime dateTime = DateTime.parse(arg, DATE_FORMAT_HMS);
        return dateTime.toDate();
    }

    public static String formatHMS(Date date) {
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(DATE_FORMAT_HMS);
    }

    public static Date parseY(String arg) {
        DateTime dateTime = DateTime.parse(arg, DATE_FORMAT_Y);
        return dateTime.toDate();
    }

    public static String formatY(Date date) {
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(DATE_FORMAT_Y);
    }

    public static String getWeekDay(Date date) {

        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;

        return weekDays[w];
    }

    /**
     * 返回 1，2，3，4，5，6，7
     *
     * @param date
     * @return
     */
    public static int getWeek(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w <= 0)
            w = 7;

        return w;
    }


    /**
     * date2比date1多的天数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDays(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if (year1 != year2)   //同一年
        {
            int timeDistance = 0;
            for (int i = year1; i < year2; i++) {
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)    //闰年
                {
                    timeDistance += 366;
                } else    //不是闰年
                {
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2 - day1);
        } else    //不同年
        {
            System.out.println("判断day2 - day1 : " + (day2 - day1));
            return day2 - day1;
        }
    }


    /**
     * 组装date，date1的年月日加上date2的时分秒
     *
     * @return
     */
    public static Date makeDate(Date date1, Date date2) {
        return parseYMDHMS(formatYMD(date1) + " " + DateUtil.formatHMS(date2));

    }

    /**
     * @return {@link }
     * @Title getCurrentZeroTime
     * @Description 获取系统当前23点59分的时间
     * @since 2018年1月23日 下午6:25:07
     */
    public static Date getCurrentZeroTime() {
        Calendar calendar = Calendar.getInstance();
        //calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 0);
        Date zero = calendar.getTime();
        return zero;
    }

    public static String getCurrentTimeStr() {
        Date date = new Date();
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(DATE_FORMAT_YMDHMS);
    }

    /**
     * 取到 hours 以前时间
     *
     * @param hours
     * @return
     */
    public static Date hoursBeforeOfDate(Date date, int hours) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR_OF_DAY, hours);
        return cal.getTime();
    }

    public static String getYesterdayStr() {
        Date date = new Date();
        date = hoursBeforeOfDate(date, -24);
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(DATE_FORMAT_YMD);
    }

    public static void main(String[] args) {
        System.out.println(getYesterdayStr());
    }

}